<template>
  <a-modal :title="title" :width="500" :visible="modalVisible" 
    @ok="handleOk"
    @cancel="handleCancel">
    <span> <a :href="tempPath+moduleName" target="_blank">点击下载模板</a>,模板标红的为必填项！</span>
    <a-upload
      name="file"
      :showUploadList="false"
      :multiple="false"
      :headers="tokenHeader"
      :action="uploadPath+moduleName"
      :data="data"
      @change="handleImportExcel"
    >
      <a-button type="primary" icon="import">导入</a-button>
    </a-upload>
  </a-modal>
</template>

<script>
import Vue from 'vue'
import JUpload from '@/components/jeecg/JUpload'
import { ACCESS_TOKEN } from "@/store/mutation-types"

export default {
  name: 'XImport',
  model: {
    prop: 'visible',
    event: 'input'
  },
  props: {
    visible: Boolean,
    data:Object,
    title: {
      type: String,
      default() {
        return '导入'
      }
    },
    moduleName: String
  },
  data() {
    return {
        modalVisible:this.visible,
        tempPath: (window._CONFIG['domianURL'] ||"")+ '/sys/common/downloadImportTemplate/',
        uploadPath:  (window._CONFIG['domianURL'] ||"") + '/sys/common/importExcel/',
        tokenHeader: { 'X-Access-Token': Vue.ls.get(ACCESS_TOKEN) },
        info:''
    }
  },
  watch:{
    visible(nv,ov){
      this.modalVisible = nv
    }
  },
  methods: {
    handleOk(){
      this.modalVisible=false
      this.$emit('input',this.modalVisible);
      this.$emit('ok',this.info)
    },
    handleCancel(){
      this.info = ''
      this.modalVisible=false
      this.$emit('cancel')
    },
    /* 导入 */
    handleImportExcel(info) {
      if (info.file.status !== 'uploading') {
        console.log(info.file, info.fileList)
      }
      if (info.file.status === 'done') {
        if (info.file.response.success) {
          // this.$message.success(`${info.file.name} 文件上传成功`);
          if (info.file.response.code === 201) {
            let {
              message,
              result: { msg, fileUrl, fileName }
            } = info.file.response
            let href = window._CONFIG['domianURL'] + fileUrl
            this.$warning({
              title: message,
              content: (
                <div>
                  <span>{msg}</span>
                  <br />
                  <span>
                    具体详情请{' '}
                    <a href={href} target="_blank" download={fileName}>
                      点击下载
                    </a>{' '}
                  </span>
                </div>
              )
            })
          } else {
            this.$message.success(info.file.response.message || `${info.file.name} 文件上传成功`)
            this.visible = false
          }
          this.info = info
          this.$emit('change',info)
          this.$emit('ok',info)
        } else {
          this.$message.error(`${info.file.name} ${info.file.response.message}.`)
        }
      } else if (info.file.status === 'error') {
        this.$message.error(`文件上传失败: ${info.file.msg} `)
      }
    }
  }
}
</script>

<style></style>
